/* -*- Mode: Actionscript; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 3 -*- */
/**
@author Timothy Uruski 
@version 1.0 2007-10-18.
@description Used by com.criticalmass.model.TweenModel to draw gradient fills on
             masking shapes, so it's pretty specialized and restricted.
**/

import flash.geom.Matrix;

import com.criticalmass.debug.Log;

class com.criticalmass.common.geom.GradientFill extends com.criticalmass.common.geom.Fill {
   /*========================================================================
   *  CONSTANTS, DEFAULTS AND PROPERTIES
   =========================================================================*/
   public static var TYPE_LINEAR:String = 'linear';
   public static var TYPE_RADIAL:String = 'radial';
   
   private var __type:String;
   private var __alphas:Array;
   private var __ratios:Array;
   private var __matrixSrc:Object;
   private var __matrix:Matrix;
   
   
   /*========================================================================
   *  ACCESSORS
   =========================================================================*/
   public function set type(ARG_type:String):Void {
      __type = (ARG_type == TYPE_LINEAR || ARG_type == TYPE_RADIAL) ? ARG_type : TYPE_LINEAR;
   }
   public function get type():String {
      return __type;
   }
   
   public function get colors():Array {
      var TMP_colors:Array = [];
      for(var i in __alphas) TMP_colors[i] = 0;
      return TMP_colors;
   }
   
   public function set alphas(ARG_alphas:String):Void {
      // Split the string into an array, trim, convert to numbers and store.
      var TMP_values:Array = ARG_alphas.split(',');
      for(var i in TMP_values) {
         TMP_values[i] = parseInt(TMP_values[i].trim());
      }
      __alphas = TMP_values;
   }
   public function get alphas():Array {
      return __alphas;
   }
   
   public function set ratios(ARG_ratios:String):Void {
      // Split the string into an array, trim, convert to numbers and store.
      var TMP_values:Array = ARG_ratios.split(',');
      for(var i in TMP_values) {
         TMP_values[i] = parseInt(TMP_values[i].trim());
      }
      __ratios = TMP_values;
   }
   public function get ratios():Array {
      return __ratios;
   }
   
   // GradientFill-matrix
   public function set matrix(ARG_matrix:String):Void {
      if(ARG_matrix != null) {
         // Split the string into an array, trim, convert to numbers and use Matrix class to create a box.
         var TMP_values:Array = ARG_matrix.split(',');
         for(var i in TMP_values) {
            TMP_values[i] = parseFloat(TMP_values[i].trim());
         }
      } else {
         // If the string is missing, create an empty set.
         TMP_values = [];
      }
      
      
      // Replace missing values with zeros.
      while(TMP_values.length < 5) TMP_values.push(0);
      // Create the matrix-source.
      __matrixSrc = {};
      __matrixSrc.w = TMP_values[0];
      __matrixSrc.h = TMP_values[1];
      __matrixSrc.r = TMP_values[2];
      __matrixSrc.x = TMP_values[3];
      __matrixSrc.y = TMP_values[4];
      generateMatrix();
   }
   public function get matrix():Matrix {
      return __matrix;
   }
   
   // Manually adjust values
   public function set matrixXpos(ARG_matrixXpos:Number):Void {
      __matrixSrc.x = ARG_matrixXpos;
      generateMatrix();
   }
   public function get matrixXpos():Number {
      return __matrixSrc.x;
   }
   public function set matrixYpos(ARG_matrixYpos:Number):Void {
      __matrixSrc.y = ARG_matrixYpos;
      generateMatrix();
   }
   public function get matrixYpos():Number {
      return __matrixSrc.y;
   }
   
   public function set matrixWidth(ARG_matrixWidth:Number):Void {
      __matrixSrc.w = ARG_matrixWidth;
      generateMatrix();
   }
   public function get matrixWidth():Number {
      return __matrixSrc.w;
   }
   public function set matrixHeight(ARG_matrixHeight:Number):Void {
      __matrixSrc.h = ARG_matrixHeight;
      generateMatrix();
   }
   public function get matrixHeight():Number {
      return __matrixSrc.h;
   }
   
   
   /*========================================================================
   *  CONSTRUCTOR, INSTANTIATION CONTROL AND LIFECYCLE
   =========================================================================*/
   public function GradientFill(ARG_type:String, ARG_alphas:String, ARG_ratios:String, ARG_matrix:String) {
      type = ARG_type;
      alphas = ARG_alphas;
      ratios = ARG_ratios;
      matrix = ARG_matrix;
   }
   
   
   /*========================================================================
   *  MATRIX CONTROLS
   =========================================================================*/
   public function adjustRatios(ARG_w:Number):Void {
      for(var i in ratios) {
         ratios[i] = ratios[i] * 255 / ARG_w;
      }
   }
   private function generateMatrix():Void {
      __matrix = new Matrix();
      __matrix.createGradientBox(__matrixSrc.w, __matrixSrc.h, __matrixSrc.r, __matrixSrc.x, __matrixSrc.y);
   }
   
   public function report():Void {
      //trace('[TU] ' + 'GradientFill: ' + type + ', [' + colors + '], [' + alphas + '], [' + ratios + '], mtx: ' + __matrix);
      Log.debug('TU@GradientFill.as', 'GradientFill: ' + type + ', [' + colors + '], [' + alphas + '], [' + ratios + '], mtx: ' + __matrix);
   }
   
   
}